package com.efast.dmom.mgmt.controller.excel;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mgmt.bean.excel.ExcelRuleDataBean;
import com.efast.dmom.mgmt.service.excel.ExcelTemplateExportCommonService;
import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.text.SimpleDateFormat;

/**
 * 
 * @author wj 2019 10/24
 *
 */
@Controller
@RequestMapping(value = "/ExcelFileUploadAndDownloadCommen")
public class ExcelTemplateExportCommonController {

	@Autowired
	private ExcelTemplateExportCommonService excelTemplateExportCommonService;

	@RequestMapping(value = "/excelTemplateDownload/{entityName}", method = RequestMethod.POST)
	public @ResponseBody void excelTemplateDownload(@PathVariable String entityName, HttpServletResponse request,
			HttpServletResponse response) throws Exception {
		String tableName = excelTemplateExportCommonService.selectTableNameByentityName(entityName);
		String fileName = tableName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		List<ExcelRuleDataBean> excelRuleDataBean = excelTemplateExportCommonService
				.selectFieldCommentByTableName(tableName);
		List<Object> fieldsData = new ArrayList<Object>();
		if (excelRuleDataBean != null && excelRuleDataBean.size() > 0) {
			for (int i = 0; i < excelRuleDataBean.size(); i++) {
				Object value = excelTemplateExportCommonService.selectFieldValueByFieldName(tableName,
						excelRuleDataBean.get(i).getFieldName());
				fieldsData.add(value);
			}

			String columnNames[] = new String[excelRuleDataBean.size()];
			for (int i = 0; i < excelRuleDataBean.size(); i++) {
				columnNames[i] = excelRuleDataBean.get(i).getFieldComment();
			}
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("sheet1");
			// 设置表格默认列宽度为15个字节
			sheet.setDefaultColumnWidth(15);
			HSSFRow row = sheet.createRow(0);
			// 在excel表中添加表头

			for (int i = 0; i < columnNames.length; i++) {
				HSSFCell cell = row.createCell(excelRuleDataBean.get(i).getExcelNumber() - 1);
				HSSFRichTextString text = new HSSFRichTextString(columnNames[i]);
				cell.setCellValue(text);
			}

			HSSFRow filedsRow = sheet.createRow(1);
			for (int i = 0; i < fieldsData.size(); i++) {
				HSSFCell cell = filedsRow.createCell(excelRuleDataBean.get(i).getExcelNumber() - 1);
				if (fieldsData.get(i) instanceof Integer) {
					cell.setCellValue((Integer) fieldsData.get(i));
				} else if (fieldsData.get(i) instanceof Float) {
					cell.setCellValue((Float) fieldsData.get(i));
				} else if (fieldsData.get(i) instanceof Double) {
					cell.setCellValue((Double) fieldsData.get(i));
				} else if (fieldsData.get(i) instanceof Long) {
					cell.setCellValue((Long) fieldsData.get(i));
				} else if (fieldsData.get(i) instanceof BigDecimal) {
					cell.setCellValue(((BigDecimal) fieldsData.get(i)).doubleValue());
				} else if (fieldsData.get(i) instanceof Date) {
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String textValue = formatter.format(fieldsData.get(i));
					cell.setCellValue(textValue);
				} else {
					// 其它数据类型都当作字符串简单处理
					if (fieldsData.get(i) != null) {
						String textValue = fieldsData.get(i).toString();
						cell.setCellValue(textValue);
					}
				}
			}

			// 清空response
			response.reset();
			// 设置response的Header
			// 告诉浏览器用什么软件可以打开此文件
			response.setContentType("application/octet-stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
			OutputStream out = response.getOutputStream();
//   	    response.setContentType("application/octet-stream;charset=utf-8");
			workbook.write(out);
			workbook.close();
			out.flush();
			out.close();
		}
	}

	// 货源规则模板下载
	@RequestMapping(value = "/barcodeRuleExcelTemplateDownload/{entityName}", method = RequestMethod.POST)
	public @ResponseBody void barcodeRuleExcelTemplateDownload(@PathVariable String entityName,
			HttpServletResponse request, HttpServletResponse response) throws Exception {
		String fileName = entityName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("sheet1");
		
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		////设置单元格水平方向对齐方式  居中
		cellStyle.setAlignment(HorizontalAlignment.LEFT);
		HSSFCellStyle cellStyle4 = workbook.createCellStyle();
		//设置字体样式
		HSSFFont font = workbook.createFont();
		font.setFontName("微软雅黑");
		//设置字号
		font.setFontHeightInPoints((short)12);
		//字体风格放入
		cellStyle4.setFont(font);
		
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		HSSFRichTextString text = new HSSFRichTextString("主键");
		cell.setCellValue(text);

		HSSFRow filedsRow = sheet.createRow(1);

		HSSFCell cell0 = filedsRow.createCell(0);
		HSSFRichTextString text0 = new HSSFRichTextString("编码名称");
		cell0.setCellValue(text0);

		HSSFCell cell1 = filedsRow.createCell(1);
		HSSFRichTextString text1 = new HSSFRichTextString("规则描述");
		cell1.setCellValue(text1);

		HSSFCell cell2 = filedsRow.createCell(2);
		HSSFRichTextString text2 = new HSSFRichTextString("编码长度");
		cell2.setCellValue(text2);

		HSSFCell cell3 = filedsRow.createCell(3);
		HSSFRichTextString text3 = new HSSFRichTextString("起始编码");
		cell3.setCellValue(text3);

		HSSFCell cell4 = filedsRow.createCell(4);
		HSSFRichTextString text4 = new HSSFRichTextString("结束编码");
		cell4.setCellValue(text4);

		HSSFCell cell5 = filedsRow.createCell(5);
		HSSFRichTextString text5 = new HSSFRichTextString("包含");
		cell5.setCellValue(text5);

		// 清空response
		response.reset();
		// 设置response的Header
		// 告诉浏览器用什么软件可以打开此文件
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
		OutputStream out = response.getOutputStream();
//		   	    response.setContentType("application/octet-stream;charset=utf-8");
		workbook.write(out);
		workbook.close();
		out.flush();
		out.close();
	}
}
